package com.aims.server.service.impl;

import com.aims.api.entity.User;
import com.aims.common.model.PageResult;
import com.aims.server.dao.mapper.UserMapper;
import com.aims.server.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 用户服务实现类
 *
 * <p>依赖 DAO：UserMapper</p>
 *
 * @author Generator
 * @since 2025-10-22
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    /**
     * 创建用户
     *
     * @param user 用户对象
     * @return 用户ID
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long create(User user) {
        if (user == null) {
            throw new IllegalArgumentException("用户对象不能为空");
        }

        int rows = userMapper.insert(user);
        if (rows > 0) {
            log.debug("用户创建成功，ID: {}", user.getId());
            return user.getId();
        }
        throw new RuntimeException("用户创建失败");
    }

    /**
     * 根据ID更新用户
     *
     * @param user 用户对象
     * @return 影响行数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(User user) {
        if (user == null || user.getId() == null) {
            throw new IllegalArgumentException("用户对象或ID不能为空");
        }

        int rows = userMapper.updateById(user);
        log.debug("用户更新完成，ID: {}, 影响行数: {}", user.getId(), rows);
        return rows;
    }

    /**
     * 根据ID删除用户
     *
     * @param id 用户ID
     * @return 影响行数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteById(Serializable id) {
        if (id == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }

        int rows = userMapper.deleteById((Long) id);
        log.debug("用户删除完成，ID: {}, 影响行数: {}", id, rows);
        return rows;
    }

    /**
     * 根据ID查询用户
     *
     * @param id 用户ID
     * @return 用户对象，不存在返回 null
     */
    @Override
    public User getById(Serializable id) {
        if (id == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }

        return userMapper.selectById((Long) id);
    }

    /**
     * 查询所有用户
     *
     * @return 用户列表
     */
    @Override
    public List<User> listAll() {
        return userMapper.selectAll();
    }

    /**
     * 统计用户总数
     *
     * @return 用户总数
     */
    @Override
    public long count() {
        Long total = userMapper.count();
        return total != null ? total : 0L;
    }

    /**
     * 分页查询用户
     *
     * @param pageNo   页码（从1开始）
     * @param pageSize 每页大小
     * @return 分页结果
     */
    @Override
    public PageResult<User> page(int pageNo, int pageSize) {
        if (pageNo < 1) {
            throw new IllegalArgumentException("页码必须大于0");
        }
        if (pageSize < 1 || pageSize > 1000) {
            throw new IllegalArgumentException("每页大小必须在1-1000之间");
        }

        // 计算偏移量
        long offset = (long) (pageNo - 1) * pageSize;

        // 查询数据
        List<User> records = userMapper.selectPage(offset, (long) pageSize);

        // 查询总数
        Long total = userMapper.count();
        long totalCount = total != null ? total : 0L;

        // 计算总页数
        long totalPages = (totalCount + pageSize - 1) / pageSize;

        return PageResult.of(records, totalCount, (long) pageNo, (long) pageSize);
    }
}